package tq.com.tqteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tq.com.tqteacher.R;

public class TQPriceSelectView extends FrameLayout {
    private GridView priceView;

    private ArrayList<HashMap<String, String>> price = new ArrayList<>();
    private int currentPrice;

    private String title = "定价";
    private String danwei = "班点";

    public TQPriceSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        initview(attrs);
    }


    private void initview(AttributeSet pAttributeSet){
        LayoutInflater.from(getContext()).inflate(R.layout.view_price_sel_layout, this);

        TypedArray typedArray = getContext().obtainStyledAttributes(pAttributeSet, R.styleable.TQPriceSelectView);
        title = typedArray.getString(R.styleable.TQPriceSelectView_TQPriceSelectView_title);
        if(title == null) title = "定价";

        danwei = typedArray.getString(R.styleable.TQPriceSelectView_TQPriceSelectView_danwei);
        if(danwei == null) danwei = "班点";
        typedArray.recycle();

        ((TextView)findViewById(R.id.title)).setText(title);
        priceView = findViewById(R.id.price_choice);
        for (int i = 0; i < 8; i++){
            HashMap<String, String> item = new HashMap<>();
            item.put("name", String.format("%d%s", i, danwei));
            item.put("value", String.valueOf(i));

            price.add(item);
        }

        final SimpleAdapter priceAdapter = new ChoiceAdapter(getContext(), price, R.layout.layout_price, new String[]{"name"}, new int[]{R.id.price_name});
        priceView.setAdapter(priceAdapter);
        priceView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentPrice = position;
                ((ChoiceAdapter) priceAdapter).setSelectPos(position);
            }
        });
    }

    public void updateData(ArrayList<String> lt){

    }

    public int getPrice(){
        return currentPrice;
    }


    public class ChoiceAdapter extends SimpleAdapter {
        public ChoiceAdapter(Context context, List<? extends Map<String, ?>> data,
                             @LayoutRes int resource, String[] from, @IdRes int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view =  super.getView(position, convertView, parent);
            view.setBackgroundResource((currentSelect == position) ? R.drawable.bg_price_select : R.drawable.bg_price_normal);
            return view;
        }

        private int currentSelect;
        public void setSelectPos(int pos){
            if(currentSelect != pos){
                currentSelect = pos;
                notifyDataSetChanged();
            }
        }
    }
}
